<template>
  <div class="g-dialog-panel">
    <div class="g-dialog-title">
      <div class="title">
        <span>Select a token </span>
        <i class="iconfont icon-help"></i>
      </div>
      <div class="g-clickable close" @click="close">
        <i class="iconfont icon-close"></i>
      </div>
    </div>
    <div class="token">
      <div class="token-search">
        <input
          type="text"
          v-model="search"
          placeholder="Search name or paste address"
        />
      </div>
      <div class="token-list g-scrollbar">
        <div
          class="token-list__item"
          v-for="(item, i) in filterTokenList"
          :key="i"
          @click="selectToken(item)"
        >
          <img :src="item.icon" class="icon" />
          <div class="info">
            <span class="symbol">{{ item.symbol }}</span>
            <span class="name">{{ item.name }}</span>
          </div>
          <div class="last">
            <span class="balance">{{ item.balanceNum }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import TokenList from "@/config/token.json";
import { mapState } from "vuex";

export default {
  data() {
    return {
      search: "",
    };
  },
  computed: {
    ...mapState({
      chainId: (state) => state.chainId,
    }),
    tokenList() {
      return TokenList[this.chainId] || [];
    },
    filterTokenList() {
      if (!this.search) return this.tokenList;

      const pattern = new RegExp(this.search || "", "gi");
      if (/^0x/.test(this.search)) {
        return this.tokenList.filter((item) => {
          const { address } = item;
          return pattern.test(address);
        });
      }
      return this.tokenList.filter((item) => {
        const { symbol, name } = item;
        return (
          pattern.test(symbol) || pattern.test(name)
        );
      });
    },
  },
  methods: {
    close() {
      this.$emit("close");
    },
    selectToken(item) {
      this.$emit("select", item);
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  padding-top: 30px;
}
.title {
  display: flex;
  align-items: center;
  justify-content: flex-start;

  i {
    cursor: pointer;
    font-size: 20px;
    color: var(--color-des);
    margin-left: 3px;
  }
}
.token-search {
  margin: 15px 0;
  input {
    width: 100%;
    height: 50px;
    padding: 15px 30px;
    border-radius: 30px;
    box-sizing: border-box;

    background-color: var(--input);
    border: 2px solid var(--input);
    outline: none;

    line-height: 30px;
    font-size: 16px;
    font-family: "Roboto Bold";
    color: var(--color);

    text-transform: uppercase;

    &:focus {
      border-color: var(--color-dark);
    }

    &::placeholder {
      text-transform: none;
      font-family: "Roboto Regular";
      color: var(--color-des);
    }
  }
}
.token-list {
  height: 405px;
  overflow: auto;
  margin: 0 -30px;

  &__item {
    cursor: pointer;
    height: 56px;
    display: grid;
    align-items: center;
    grid-gap: 8px;
    grid-template-columns: 30px 120px 1fr;
    padding: 0 37px 0 30px;

    &:hover {
      background-color: rgba(255, 255, 255, 0.03);
    }

    .icon {
      width: 30px;
      border-radius: 15px;
    }

    .info {
      display: flex;
      flex-direction: column;
      align-items: flex-start;

      .symbol {
        color: var(--color);
        font-size: 14px;
        font-family: "Roboto Bold";
        margin-bottom: 5px;
      }

      .name {
        color: var(--color-content);
        font-size: 12px;
        font-family: "Roboto Regular";
      }
    }

    .last {
      display: flex;
      align-items: center;
      justify-content: flex-end;

      span {
        font-size: 14px;
        font-family: "Roboto Medium";
        color: var(--color-content);
      }
    }
  }
}
</style>
